package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.EmpNoticeAddDTO;
import org.jsola.hr.dto.EmpNoticeUpdateDTO;
import org.jsola.hr.query.EmpNoticeQuery;
import org.jsola.hr.service.IEmpNoticeService;
import org.jsola.hr.vo.EmpNoticeListVO;
import org.jsola.hr.vo.EmpNoticeVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 员工通知
 *
 * @author zhr
 */
@RestController("hrEmpNoticeController")
@RequestMapping(HrConstants.API_PATH + "/v1/empNotices")
public class EmpNoticeController implements IEmpNoticeControllerAPI {

    @Autowired
    private IEmpNoticeService empNoticeService;

    @Override
    @PostMapping
    public Result<EmpNoticeVO> save(@Valid @RequestBody EmpNoticeAddDTO empNoticeAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工通知权限校验

        return Result.success(
                empNoticeService.save(empNoticeAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody EmpNoticeUpdateDTO empNoticeUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改员工通知权限校验

        int count = empNoticeService.update(empNoticeUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{empNoticeId}")
    public Result<Integer> delete(@PathVariable String empNoticeId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除员工通知权限校验

        int count = empNoticeService.deleteByIds(tokenUser, empNoticeId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] empNoticeIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工通知权限校验

        int count = empNoticeService.deleteByIds(tokenUser, empNoticeIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{empNoticeId}")
    public Result<EmpNoticeVO> findById(@PathVariable String empNoticeId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工通知权限校验

        return Result.success(
                empNoticeService.selectById(empNoticeId, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<EmpNoticeListVO>> listAll(@Valid EmpNoticeQuery empNoticeQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工通知权限校验

        return Result.success(
                empNoticeService.select(empNoticeQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<EmpNoticeListVO>> page(@Valid EmpNoticeQuery empNoticeQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                empNoticeService.selectPage(empNoticeQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listUserNoticeGroupByUser/{companyId}")
    public Result<List<EmpNoticeListVO>> listUserNoticeGroupByUser(@PathVariable String companyId,
                                                                   @Valid EmpNoticeQuery empNoticeQuery) {
        TokenUser tokenUser = UserContext.getUser();
        empNoticeQuery.setCompanyId(companyId);
        return Result.success(
                empNoticeService.listLastOneUserNotice(empNoticeQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/pageUserNoticeGroupByUser/{companyId}")
    public Result<Page<EmpNoticeListVO>> pageUserNoticeGroupByUser(@PathVariable String companyId,
                                                                   @Valid EmpNoticeQuery empNoticeQuery) {
        TokenUser tokenUser = UserContext.getUser();
        empNoticeQuery.setCompanyId(companyId);
        return Result.success(
                empNoticeService.pageLastOneUserNotice(empNoticeQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listNoticeByUser/{companyId}")
    public Result<List<EmpNoticeListVO>> listNoticeByUser(@PathVariable String companyId,
                                                          @Valid EmpNoticeQuery empNoticeQuery) {
        TokenUser tokenUser = UserContext.getUser();
        empNoticeQuery.setCompanyId(companyId);
        return Result.success(
                empNoticeService.select(empNoticeQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/pageNoticeByUser/{companyId}")
    public Result<Page<EmpNoticeListVO>> pageNoticeByUser(@PathVariable String companyId,
                                                          @Valid EmpNoticeQuery empNoticeQuery) {
        TokenUser tokenUser = UserContext.getUser();
        empNoticeQuery.setCompanyId(companyId);
        return Result.success(
                empNoticeService.selectPage(empNoticeQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listNoticeCurrentUser/{companyId}")
    public Result<List<EmpNoticeListVO>> listNoticeCurrentUser(@PathVariable String companyId,
                                                               @Valid EmpNoticeQuery empNoticeQuery) {
        TokenUser tokenUser = UserContext.getUser();
        empNoticeQuery.setCompanyId(companyId);
        empNoticeQuery.setPublishUserId(tokenUser.getUserId());
        return Result.success(
                empNoticeService.select(empNoticeQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/pageNoticeCurrentUser/{companyId}")
    public Result<Page<EmpNoticeListVO>> pageNoticeCurrentUser(@PathVariable String companyId,
                                                               @Valid EmpNoticeQuery empNoticeQuery) {
        TokenUser tokenUser = UserContext.getUser();
        empNoticeQuery.setCompanyId(companyId);
        empNoticeQuery.setPublishUserId(tokenUser.getUserId());
        return Result.success(
                empNoticeService.selectPage(empNoticeQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/findUserNotice/{userNoticeId}")
    public Result<EmpNoticeListVO> findUserNotice(@PathVariable String userNoticeId) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                empNoticeService.findUserNotice(userNoticeId, tokenUser)
        );
    }

    @Override
    @DeleteMapping(value = "/deleteUserNotice/{userNoticeId}")
    public Result<String> deleteUserNotice(@PathVariable String userNoticeId) {
        TokenUser tokenUser = UserContext.getUser();

        empNoticeService.deleteByIds(tokenUser, userNoticeId);

        return Result.success("删除成功");
    }

    @Override
    @DeleteMapping(value = "/deleteUserNoticeBatch")
    public Result<String> deleteUserNoticeBatch(@RequestBody String[] userNoticeIds) {
        TokenUser tokenUser = UserContext.getUser();

        empNoticeService.deleteByIds(tokenUser, userNoticeIds);

        return Result.success("删除成功");
    }

    @Override
    @PutMapping(value = "/readUserNotice/{userNoticeId}")
    public Result<String> readUserNotice(@PathVariable String userNoticeId, @RequestParam String empId) {
        TokenUser tokenUser = UserContext.getUser();

        empNoticeService.readUserNotice(tokenUser, userNoticeId, empId);

        return Result.success("修改成功");
    }

    @Override
    @PutMapping(value = "/batchReadUserNotice/{userNoticeId}")
    public Result<String> readUserNoticeBatch(@PathVariable String userNoticeId,
                                              @RequestBody String[] empIds) {
        TokenUser tokenUser = UserContext.getUser();

        empNoticeService.readUserNotice(tokenUser, userNoticeId,empIds);

        return Result.success("修改成功");
    }


}

